<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/12
 * Time: 9:27
 */
namespace Admin\Controller;
use Think\Controller;

class CateController extends CommonController
{
    private $cate='';

    function __construct()
    {
        parent::__construct();
        $this->cate=D("Category");
        $this->cate->startTrans();
    }

    function addCate(){
        if($_POST)
        {
            $data=$_POST;
            $data['id']='';
            $data['add_time']=time();
            $data['updata_time']=time();
            $aa=$this->cate->data($data)->add();
            if($aa==true){
                $this->cate->commit();
                $this->success("栏目添加成功");
            }else{
                $this->cate->rollback();
                $this->error("栏目添加失败");
            }
        }else{
            $cates=$this->cate->select();
            $datas=$cates;

            foreach($cates as $key=>$r)
            {
                $datas[$key]['add_time']=date("Y-m-d H:i:s",$r['add_time']);
                $datas[$key]['updata_time']=date("Y-m-d H:i:s",$r['updata_time']);
            }

            $this->assign("data",$datas);
            $this->display();
        }
    }

    function editCate($id='')
    {
        if($_POST)
        {
            $data=$_POST;
            $data['add_time']=time();
            $data['updata_time']=time();
            $cond['id']=$id;
            $aa=$this->cate->where($cond)->save($data);
            if($aa==true){
                $this->cate->commit();
                $this->success("栏目修改成功");
            }else{
                $this->cate->rollback();
                $this->error("栏目修改失败");
            }
        }else{

            $cond['id']=$id;
            $arr=$this->cate->where($cond)->find();
            $this->assign("data",$arr);
            $this->display();
        }
    }

    function delCate($id=''){
        $aa=$this->cate->where("id=".$id)->delete();
        if($aa==true){
            $this->cate->commit();
            $this->success("数据删除成功!!!");
        }else{
            $this->cate->rollback();
            $this->error("数据删除失败!!!");
        }
    }
}